package com.xy.blog.user.dao.impl;

import com.xy.blog.core.dao.IBaseDao;
import com.xy.blog.core.exception.BaseException;
import com.xy.blog.user.dao.IUserOrgDao;
import com.xy.blog.user.dao.mapper.rel.UserOrgMapper;
import com.xy.blog.user.dao.model.rel.UserOrgDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author zanhonglei
 * @version V1.0
 * @Description: 组织机构dao
 * @date 2020/3/4
 */
@Repository
public class UserOrgDaoImpl implements IBaseDao<UserOrgDO> , IUserOrgDao {


    @Autowired
    private UserOrgMapper userOrgMapper;


    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void save(UserOrgDO item) throws BaseException {
        userOrgMapper.insert(item);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void updateById(UserOrgDO item) throws BaseException {
        userOrgMapper.updateByPrimaryKeySelective(item);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void deleteById(Long id) throws BaseException {
        userOrgMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void deleteByUserId(Long id) throws BaseException {
        UserOrgDO userOrgDO = new UserOrgDO();
        userOrgDO.setUserId(id);
        userOrgMapper.delete(userOrgDO);
    }

    @Override
    public UserOrgDO getById(Long id) throws BaseException {
        return userOrgMapper.selectByPrimaryKey(id);
    }

    public UserOrgDO getByUserId(Long id) throws BaseException {
        UserOrgDO userOrgDO = new UserOrgDO();
        userOrgDO.setUserId(id);
        return userOrgMapper.selectOne(userOrgDO);
    }
}
